#Figure 2: Local development levels
rm(list=ls(all=T))
p_needed <- c("dplyr", "ggplot2","sf", "rmapshaper","ggthemes")
lapply(p_needed, require, character.only = TRUE)

# African Continent Shapefile
africa <- st_read("cs_africa/cs_africa.shp")%>%
  rmapshaper::ms_simplify(keep=0.1) %>%
  st_transform("+proj=aea +lat_1=20 +lat_2=-23 +lat_0=0 +lon_0=25 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs") %>%
  mutate(iso_code = countrycode::countrycode(statename,"country.name","iso3c"))


ab_countries <- c("Algeria", "Benin", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde", "Ivory Coast", "Egypt", 
                  "Ghana", "Guinea", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritius", "Morocco",
                  "Mozambique", "Namibia", "Niger", "Nigeria", "Senegal", "Sierra Leone", "South Africa", "Sudan", "Swaziland", 
                  "Tanzania", "Togo", "Tunisia", "Uganda", "Zambia", "Zimbabwe", "Sao Tome and Principe")
ab_countries <- countrycode::countrycode(ab_countries,"country.name","iso3c")

africa <-  africa %>% mutate(afrocountry = as.factor(ifelse(iso_code %in% ab_countries, "Afrobarometer Country", "Not in Afrobarometer"))) 

# Local Development Levels of Afrobarometer Survey Clusters
eas <- readRDS("data/eas_development.Rds")
breaks <- quantile(eas$light_30, na.rm=T)

# Figure 2
ggplot() +
  geom_sf(data = africa, aes(fill= afrocountry), color = "gray60") +
  scale_fill_manual(values = c("gray60","gray80"), guide = guide_legend(title = NULL)) + 
  ggnewscale::new_scale_fill() + 
  geom_sf(data = africa, fill= NA, color = "white", size = 0.5) +
  geom_sf(data = eas, aes(fill=light_30), color=NA) +
  labs(caption = "Dots: 30km buffers around Afrobarometer survey cluster centroids (5th wave) filled with 2010 night lights") +
  coord_sf() +
  theme(legend.position = "bottom", legend.box="vertical", legend.margin=margin(),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.major = element_line(color = "white", size = 0.1),
        panel.grid.minor = element_blank(),
        plot.background = element_blank(), 
        panel.background = element_blank(), 
        legend.background = element_blank(),
        panel.border = element_blank()) +
  scale_fill_gradientn(colours = c("#001a4d", "#0066FF", "yellow"),
                       values = scales::rescale(breaks), 
                       name = "Night Lights (DN) of Afrobarometer Survey Clusters",
                       guide = guide_colorbar(
                         direction = "horizontal",
                         barheight = unit(2, units = "mm"),
                         barwidth = unit(50, units = "mm"),
                         draw.ulim = F,
                         title.position = 'top',
                         # some shifting around
                         title.hjust = 0.5,
                         label.hjust = 0.5))

# 